package control 
{
	import base.constants.Events;
	import flash.display.Sprite;
	import flash.geom.Rectangle;
	import flash.events.Event;
	import tweens.Tween;
	import appkit.responders.NResponder;
	
	public class Camera extends Sprite 
	{
		public static const rectD:Rectangle = new Rectangle( -600, -600, 1600, 1600);
		public static const rectC:Rectangle = new Rectangle( -600, -600, 1201, 1201);
		
		private var delay:Number = .8;
		private var step:uint = 15;
		private var move:uint = 200;
		private var limit:uint = 1;
		private var szoom:int = 0;
		private var zoom:Number = .2;
		
		
		public function zoomLevel():int
		{
			return szoom;
		}
		
		private function panH(f:int):void
		{
			var g:int = this.x + (f * move);
			if (rectC.contains(g, this.y))
			{
				var t:Tween = new Tween(this, { x: { start:this.x, end:g, steps:step }}, Tween.circleInOut);
				t.start();
			}
		}
		
		private function panV(f:int):void
		{
			var g:int = this.y + (f * move);
			if (rectC.contains(this.x, g))
			{
				var t:Tween = new Tween(this, { y: { start:this.y, end:g, steps:step }}, Tween.circleInOut);
				t.start();
			}
		}
		
		private function zoomCamera(f:int):void
		{
			szoom += f;
			var t:Tween = new Tween(this, { scaleX: { start:this.scaleX, end:this.scaleX + (f * zoom), steps:step }, scaleY: { start:this.scaleY, end:this.scaleY + (f * zoom), steps:step }}, Tween.circleInOut);
			t.start();
		}
		
		
		private function on_cameramove(dir:int = 0):void
		{
			switch(dir)
			{
				case 0: panH(1); break;
					
				case 1: panV(1); break;
				
				case 2: panH( -1); break;
				
				case 3: panV( -1); break;
			}
		}
		
		private function on_camerazoom(dir:int = 0):void
		{
			trace(dir);
			switch(dir)
			{
				case -1:
					if (szoom > -limit)
						zoomCamera(dir);
					break;
					
				case 1:
					if (szoom < limit)
						zoomCamera(dir);
					break;
			}
		}
		
		
		private function init(e:Event):void
		{
			NResponder.add(Events.Camera_Move, on_cameramove, 0, delay);
			NResponder.add(Events.Camera_Zoom, on_camerazoom, 0, delay);
		}
		
		public function Camera() 
		{
			NResponder.addNative(this, Event.ADDED_TO_STAGE, init, 1);
		}
	}

}